let dayjs = require('dayjs');
Component({
  data: {
    listData: [],
    selectList: [],
  },
  props: {
    endNode: false, //仅子节点可选 true:是,false:否
    _key: 0,
    level: 0,
    treeData: [],
    onChengCheck: (data) => console.log(data),
  },
  didMount: function didMount() { //页面渲染完毕后执行 (组件的生命周期函数)
    // console.log(this.props.endNode)
    this.setData({
      endNode: this.props.endNode
    })
    this.init()
  },
  didUpdate(prevProps, prevData) {
    this.init()
  },
  methods: {
    init() {
      this.data.listData = this.dataRender(this.props.treeData, 0, 0)
      this.setData({
        listData: this.data.listData
      })
    },

    // 初始化数据
    dataRender(data, level, _key) {
      for (let i = 0; i < data.length; i++) {
        data[i].level = level
        data[i]._key = _key + '-' + i
        data[i].unfold = true
        if (data[i].children != undefined && data[i].children.length > 0) {
          let l = level + 1
          this.dataRender(data[i].children, l, data[i]._key)
        }
      }
      return data
    },

    // 选择渲染
    checkRender(data, _key) {
      let findI = data.findIndex(n => {
        return n._key == _key
      })
      if (findI == -1) {
        for (let i = 0; i < data.length; i++) {
          if (data[i].children != undefined && data[i].children.length > 0) {
            this.checkRender(data[i].children, _key)
          }
        }
      } else {
        data[findI].checked = !data[findI].checked
        if (data[findI].checked) {
          let obj = {
            _key: data[findI]._key,
            key: data[findI].key,
            level: data[findI].level,
            title: data[findI].title,
            value: data[findI].value
          }
          // this.data.selectList.push(obj)
          this.data.selectList = [obj]
        } else {
          let findlis = this.data.selectList.findIndex(n => {
            return n.key == data[findI]._key
          })
          if (findlis != -1) {
            this.data.selectList.splice(findlis, 1);
          }
        }
      }
      return data
    },

    // 选择渲染
    unfoldRender(data, _key) {
      let findI = data.findIndex(n => {
        return n._key == _key
      })
      if (findI == -1) {
        for (let i = 0; i < data.length; i++) {
          if (data[i].children != undefined && data[i].children.length > 0) {
            this.unfoldRender(data[i].children, _key)
          }
        }
      } else {
        data[findI].unfold = !data[findI].unfold
      }
      return data
    },

    treeCheck(e) {
      let item = e.target.dataset.item
      let newData = this.checkRender(this.data.listData, item._key)
      this.setData({
        listData: newData
      })
      this.onChengCheck()
    },

    isunfold(e) {
      let item = e.target.dataset.item
      let newData = this.unfoldRender(this.data.listData, item._key)
      this.setData({
        listData: newData
      })
    },

    onChengCheck(e) {
      // console.log(this.data.selectList)
      this.props.onChengCheck(this.data.selectList);
    },

  }
});